package com.sinosoft.ptas.control;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.IndexData;
import com.sinosoft.ptas.control.dto.IndexDatas;
import com.sinosoft.ptas.entity.IndexEntity;
import com.sinosoft.ptas.service.IndexService;
import com.sinosoft.ptas.system.commom.CommonConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;
/**
 * 指标控制层
 * @author lianWuJun
 * @time   2017年2月21日 上午9:24:52
 */
@Controller
@RequestMapping("index")
public class IndexController {

	@Autowired
	IndexService indexService;
	
	/**
	 * 创建指标
	 * @param indexData
	 * @return 指标传输类或false
	 */
	@RequestMapping(value="/createIndex", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData createIndex(IndexData indexData){
		if(indexData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
			IndexEntity indexEntity=indexData.toEntity();
			String id=indexService.createIndex(indexEntity);
			indexData.setId(id);
			return new ResponseData(true,indexData);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
	}
	
	/**
	 * 根据指标编号查询指标
	 * @param id
	 * @return 指标传输类
	 */
	@RequestMapping(value="/retrieveIndexById",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveIndexById(String id){
		if(StringUtil.isBlank(id)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		IndexEntity indexEntity=indexService.retrieveIndex(id);
		IndexData indexData=toIndexData(indexEntity);
		return new ResponseData(true,indexData);
	}
	
	/**
	 * 根据指标类的属性查询指标(审核表编号)
	 * @param indexData
	 * @return 指标传输类集合
	 */
	@RequestMapping(value="/retrieveIndexs",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveIndexs(IndexData indexData){
		IndexEntity indexEntity=indexData.toEntity();
		List<IndexEntity> indexEntitys=indexService.retrieveIndexs(indexEntity);
		List<IndexData> indexDatas=toIndexDatas(indexEntitys);
		return new ResponseData(true,indexDatas);	
	}
	
	/**
	 * 更新指标
	 * @param indexData
	 * @return 指标传输类或false
	 */
	@RequestMapping(value="/updateIndex",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateIndex(IndexData indexData){
		if(indexData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
			IndexEntity indexEntity=indexData.toEntity();
			indexService.updateIndex(indexEntity);
			return new ResponseData(true,indexData);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);
		}	
	}
	/**
	 * 批量更新指标
	 * @param indexData
	 * @return 指标传输类或false
	 */
	@RequestMapping(value="/updateIndexs",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateIndexs(@RequestBody IndexDatas indexDatas){
		if(indexDatas==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		String statu = indexDatas.getStatu();
	    try {
	    	List<IndexEntity> indexEntitys = toIndexEntitys(indexDatas.getIndexDatas());
			indexService.updateIndexs(indexEntitys,statu);
			return new ResponseData(true,indexEntitys);
		} catch (BusinessException e) {
			e.printStackTrace();
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
		
	}
	/**
	 * 删除指标
	 * @param id
	 * @return true或false
	 */
	@RequestMapping(value="/deleteIndexById",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteIndexById(String id){
		if(StringUtil.isBlank(id)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		try{
			indexService.deleteIndex(id);
			return new ResponseData(true,CommonConstant.DELETE_SUCCESS);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}			
	}
	
	/**
	 * @param indexEntity
	 * @return 指标实体类转化为传输类
	 */
	private IndexData toIndexData(IndexEntity indexEntity) {
		IndexData indexData = new IndexData();
		indexData.toindexData(indexEntity);
		return indexData;
	}
	
	/**
	 * 指标数据转换格式集合转换
	 * @param indexs
	 * @return 指标集合传输类
	 */
	private List<IndexData> toIndexDatas(List<IndexEntity> indexs) {
		int size = indexs.size();
		List<IndexData> indexDatas = new ArrayList<IndexData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			IndexData indexData = new IndexData();
			indexData.toindexData(indexs.get(i));
			indexDatas.add(indexData);
		}
		return indexDatas;
	}

	private List<IndexEntity> toIndexEntitys(List<IndexData> indexs) {
		int size = indexs.size();
		List<IndexEntity> indexEntitys = new ArrayList<IndexEntity>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			indexEntitys.add(indexs.get(i).toEntity());
		}
		return indexEntitys;
	}
}
